package cn.kgc.online.edu.controller.admin;

import cn.kgc.online.common.vo.R;
import cn.kgc.online.edu.entity.Teacher;
import cn.kgc.online.edu.query.TeacherQuery;
import cn.kgc.online.edu.service.TeacherService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(description = "讲师管理")
@CrossOrigin
@RestController
@RequestMapping("/admin/edu/teacher")
public class TeacherAdminController {

    @Resource
    TeacherService teacherService;

    @ApiOperation(value = "所有讲师列表")
    @GetMapping("/list")
    public R list() {
        String a=null;
        System.out.println("a.equals(\"\") = " + a.equals(""));
   /*     if (a==null){
            throw new KgcOnlineException("a不能为空",21008);
        }*/

        List<Teacher> list = teacherService.list(null);

        return R.ok().code(2000).data("items", list);

    }

    @ApiOperation(value = "根据id逻辑删除讲师")
    @DeleteMapping("/del/{id}")
    public R del(@PathVariable("id") @ApiParam(name = "id", value = "讲师ID", required = true) String id) {
        teacherService.removeById(id);
        return R.ok();
    }

    @ApiOperation(value = "讲师列表分页")
    @GetMapping("/{page}/{limit}")
    public R pageQuery(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable("page") Integer page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable("limit") Integer limit,
            @ApiParam(name = "teacherQuery", value = "查询对象", required = false)
                    TeacherQuery teacherQuery) {

        Page<Teacher> teacherPage = new Page<>(page, limit);
        teacherService.pageQuery(teacherPage, teacherQuery);
        List<Teacher> records = teacherPage.getRecords();
        long total = teacherPage.getTotal();
        return R.ok().data("total", total).data("records", records);
    }

    @ApiOperation(value = "新增教师")
    @PostMapping("/add")
    public R save(@ApiParam(name = "teacher", value = "讲师对象", required = true)
                  @RequestBody Teacher teacher
    ) {
        teacherService.save(teacher);
        return R.ok();
    }

    @ApiOperation(value = "根据教师ID查询")
    @GetMapping("/{id}")
    public R getById(
            @ApiParam(name = "id", value = "教师id", required = true)
            @PathVariable("id") String id) {

        Teacher teacher = teacherService.getById(id);
        return R.ok().data("items", teacher);
    }


    @ApiOperation(value = "根据id修改教师信息")
    @PutMapping("/{id}")
    public R updTeacher(
            @ApiParam(name = "id", value = "教师id", required = true)
            @PathVariable("id") String id,
            @ApiParam(name = "teacher", value = "教师对象", required = true)
            @RequestBody Teacher teacher
    ) {
        teacher.setId(id);
        teacherService.updateById(teacher);
        return R.ok();
    }

}
